/**
 * @file:container_of.c
 * @brief:使用container_of函数
 * @author:liaowx
 * @date:2019.08.02
 */
#include <stdio.h>

#define offsetof(TYPE,MEMBER)   ((size_t) &((TYPE *)0)->MEMBER)
#define container_of(PTR,TYPE,MEMBER)    ({  \
    const typeof(((TYPE *)0)->MEMBER) *__mptr=(PTR);  \
    (TYPE *) ((char *)__mptr - offsetof(TYPE,MEMBER)); })

int main(int argc, char *argv[])
{
    struct test {
        int num;
        char ch;
    }t1 = {100, 'c'};
    char *pch = &t1.ch;
    struct test *ptt = container_of(pch, struct test, ch);
    printf("num = %d\n", ptt->num);

    return 0;
}